package com.jk.admin.service;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.jk.admin.dao.LoginDao;
import com.jk.admin.dao.TreeDao;
import com.jk.admin.entity.MongUser;
import com.jk.admin.entity.RoleEntity;
import com.jk.admin.entity.TreeEntity;
import com.jk.admin.entity.User;
import com.jk.admin.utils.ReturnMsgUtils;
import com.mongodb.client.result.UpdateResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TreeService {
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private LoginDao loginDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;
    public List<TreeEntity> getTree(Integer pid) {
        List<TreeEntity> list=treeDao.getTree(pid);
        for (int i = 0; i < list.size(); i++) {
            List<TreeEntity> list2=getTree(list.get(i).getId());
            list.get(i).setNodes(list2);
        }
        return  list;
    }

    public Map login(User user, HttpServletRequest request) {
        if (user.getUser_name()==null || "".equals(user.getUser_name())){
            return ReturnMsgUtils.returnMsg(201,"账号为空",null);
        }
        if (user.getUser_password()==null || "".equals(user.getUser_password())){
            return ReturnMsgUtils.returnMsg(202,"密码为空",null);
        }else{
            User user1=loginDao.login(user);
            if(user1==null){
                return  ReturnMsgUtils.returnMsg(203,"账号或者密码错误",null);
            }else{
                if(user1.getUser_state()==1){
                    if(user1.getUser_type()>=3){
                    SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Query query1=new Query();
                    query1.addCriteria(Criteria.where("user_name").is(user.getUser_name()).andOperator(Criteria.where("user_password").is(user.getUser_password())));
                    MongUser one1 = mongoTemplate.findOne(query1, MongUser.class);
                    System.out.println(one1);
                    Query query=new Query();
                    query.addCriteria(Criteria.where("id").is(one1.getId()));
                    Update update=new Update();
                    update.set("login_time",sim.format(new Date()));
                    mongoTemplate.upsert(query,update,MongUser.class);
                    request.getSession().setAttribute("user",user1);
                    return  ReturnMsgUtils.returnMsg(200,"登录成功",user1);
                    }else {
                        return  ReturnMsgUtils.returnMsg(208,"您的权限不够，请切换账号重新登录",user1);
                    }
                }else{
                    return  ReturnMsgUtils.returnMsg(204,"账号已被冻结,请解封",user1);
                }
            }
        }
    }

/*    public void addUser(User user ,HttpServletRequest request) {
        user.setUser_name(user.getUser_name1());
        user.setUser_password(user.getUser_password1());
        user.setUser_type(2);
        user.setUser_state(1);
        System.out.println(user+"111");
        loginDao.addUser(user);
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setAdd_time(sim.format(new Date()));
        MongUser info=new MongUser();
        info.setUser_id(user.getUser_id());
        info.setAdd_time(sim.format(new Date()));
        info.setUser_name(user.getUser_name());
        info.setUser_password(user.getUser_password());
        info.setUser_type(user.getUser_type());
        info.setUser_email(user.getUser_email());
        info.setUser_nickname(user.getUser_nickname());
        info.setLogin_time("");
        info.setUser_state(1);
        System.out.println(user);
        mongoTemplate.save(info);
    }*/

    public Object getUser(MongUser user) {
        Query query=new Query();
        Criteria cc = new Criteria();
        query.addCriteria(cc);
        if(!StringUtils.isEmpty(user.getUser_name())){
            cc.orOperator(Criteria.where("user_name").regex(user.getUser_name()),Criteria.where("user_nickname").regex(user.getUser_name()));
        }
        List<MongUser> list = mongoTemplate.find(query, MongUser.class);
        return  list;
    }

    public MongUser getHui(String id) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
        MongUser one = mongoTemplate.findOne(query, MongUser.class);
        System.out.println(one);
        return  one;
    }

    public void update(User user) {
        loginDao.update(user);
        Query query=new Query();
        query.addCriteria(Criteria.where("user_id").is(user.getUser_id()));
        Update update=new Update();
        update.set("user_name",user.getUser_name());
        update.set("user_password",user.getUser_password());
        update.set("user_nickname",user.getUser_nickname());
        update.set("user_email",user.getUser_email());
        update.set("user_state",user.getUser_state());
        update.set("info",user.getInfo());
        mongoTemplate.upsert(query,update,MongUser.class);
    }

    public void del(Integer user_id,String id) {
        loginDao.del(user_id);
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
        mongoTemplate.remove(query,MongUser.class);


    }

    public void add(User user) {
        SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        user.setCount(99);
        user.setUser_state(1);
        user.setUser_type(2);
        loginDao.add(user);

        MongUser mongUser=new MongUser();
        mongUser.setUser_name(user.getUser_name());
        mongUser.setUser_id(user.getUser_id());
        mongUser.setUser_email(user.getUser_email());
        mongUser.setUser_nickname(user.getUser_nickname());
        mongUser.setUser_state(user.getUser_state());
        mongUser.setUser_password(user.getUser_password());
        mongUser.setUser_id(user.getUser_id());
        mongUser.setUser_type(user.getUser_type());
        mongUser.setLogin_time("");

        mongUser.setAdd_time(sim.format(new Date()));

        mongUser.setCount(99);
        mongUser.setInfo(user.getInfo());
        mongUser.setRole_name("");
        mongoTemplate.save(mongUser);

    }

    public void qi(Integer user_id,String id) {
        loginDao.qi(user_id);

       /* Query query=new Query();
        query.addCriteria(Criteria.where("user_id").is(user_id));

        MongUser one = mongoTemplate.findOne(query, MongUser.class);
        System.out.println(one);*/

        Query query2=new Query();
        query2.addCriteria(Criteria.where("id").is(id));
        Update update=new Update();
        update.set("user_state",1);
        mongoTemplate.upsert(query2,update,MongUser.class);
    }

    public void guan(Integer user_id,String id) {
        loginDao.guan(user_id);
  /*      Query query=new Query();
        query.addCriteria(Criteria.where("user_id").is(user_id));

        MongUser one = mongoTemplate.findOne(query, MongUser.class);*/

        Query query2=new Query();
        query2.addCriteria(Criteria.where("id").is(id));
        Update update=new Update();
        update.set("user_state",2);
        mongoTemplate.upsert(query2,update,MongUser.class);
    }

    public void update2(MongUser user) {
        loginDao.update2(user.getUser_id(),user.getUser_type());
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(user.getId()));
        Update update=new Update();
        update.set("user_type",user.getUser_type());
        if(user.getUser_type()==0){
            update.set("role_name","游客");
        }
        if(user.getUser_type()==1){
            update.set("role_name","买家");

        }
        if(user.getUser_type()==2){
            update.set("role_name","卖家");
        }
        if(user.getUser_type()==3){
            update.set("role_name","未定义");
        }
        if(user.getUser_type()==4){
            update.set("role_name","管理员");
        }
        if(user.getUser_type()==5){
            update.set("role_name","超级管理员");
        } else if(user.getUser_type()==6){
            update.set("role_name","订单管理员");
        }else{
             update.set("role_name","商品管理员");
        }

        mongoTemplate.upsert(query,update,MongUser.class);


    }

    public  MongUser getHui2(String id){
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(id));
      return   mongoTemplate.findOne(query,MongUser.class);
    }

    public List<MongUser> getRole(MongUser mongUser) {
        Query query=new Query();
        if(!StringUtils.isEmpty(mongUser.getRole_name())){
            query.addCriteria(Criteria.where("role_name").regex(mongUser.getRole_name()));
        }
        query.addCriteria(Criteria.where("user_type").gt(2));
        return   mongoTemplate.find(query,MongUser.class);
    }

    public void updateRole(MongUser user) {
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(user.getId()));
        Update update=new Update();
        update.set("info",user.getInfo());
        update.set("user_state",user.getUser_state());
        mongoTemplate.upsert(query,update,MongUser.class);
    }
}
